//文件大小转换
function TransByte($Abyte) {
    $KB = 1024;
    $MB = 1024 * $KB;
    $GB = 1024 * $MB;
    $TB = 1024 * $GB;
    if ($Abyte < $KB) {
        return $Abyte + "B";
    } else if ($Abyte < $MB) {
        return ($Abyte / $KB).toFixed(2) + "KB";
    } else if ($Abyte < $GB) {
        return ($Abyte / $MB).toFixed(2) + "MB";
    } else if ($Abyte < $TB) {
        return ($Abyte / $GB).toFixed(2) + "GB";
    } else {
        return ($Abyte / $TB).toFixed(2) + "TB";
    }
}

//JS检测文件大小是否允许
function checkfilesize($AInputID, $AFileSize) {
    var str = document.getElementById($AInputID).value;
    if (str == "") {
        alert("请选择要上传的文件！");
        return false;
    }
    var f = document.getElementById($AInputID).files;
    if (f[0].size > $AFileSize) {
        document.getElementById("ErrorMSG").innerHTML = '【"' + f[0].name + '"共' + TransByte(f[0].size) + '，最大只支持20M的文件！】';
        return false;
    }
}

//编辑框限制输入长度限制，AID为ID名, ALength为长度
function LimitInputLength(AID, ALength) {
    var text = document.getElementById(AID);
    text.onkeyup = function () {
        //this.value = this.value.replace(/\D/g, '');
        if (text.value.length > ALength) {
            text.value = '';
        }
    }
}

//编辑框限制只能输入数字，AID为ID名, ALength为长度
function LimitOnlyNOInputLength(AID, ALength) {
    var text = document.getElementById(AID);
    text.onkeyup = function () {
        this.value = this.value.replace(/\D/g, '');
        if (text.value.length > ALength) {
            text.value = '';
        }
    }
}

function F_Operate_Disable(AID, ADisable) {
    var m_Element = document.getElementById(AID);
    m_Element.disabled = ADisable;
}

function F_Operate_ReadOnly(AID, AReadOnly) {
    var m_Element = document.getElementById(AID);
    m_Element.readOnly = AReadOnly;
}


//操作选择下拉框，选中的内容为Text
function F_Operate_Select(AID, AText) {
    var select = document.getElementById(AID);
    for (var i = 0; i < select.options.length; i++) {
        if (select.options[i].innerHTML == AText) {
            select.options[i].selected = true;
            break;
        }
    }
}

//操作CheckBox勾选
function F_Operate_Check(AName, AText) {
    var val = AText.split(",");

    var boxes = document.getElementsByName(AName);
    for (i = 0; i < boxes.length; i++) {
        for (j = 0; j < val.length; j++) {
            if (boxes[i].value == val[j]) {
                boxes[i].checked = true;
                break
            }
        }
    }
}

//弹出一个HTML窗口
function OpenLayerHTMLWindow(AID, ACaption, AUrl, AWidth, ALength, AReload) {
    $j(AID).on('click', function () {
        layer.open({
            type: 2,
            title: ACaption,
            shadeClose: false,
            shade: 0.7,//透明度
            area: [AWidth, ALength],
            maxmin: true,
            fixed: false, //不固定
            moveOut: true,
            content: AUrl,
            end: function () {
                //默认参数为0，不刷心
                AReload = AReload || 0;
                if (AReload == 1) {
                    window.location.reload();
                }
            }
        });
    });
}

//弹出一个HTML窗口
function ShowLayerHTMLWindow(ACaption, AUrl, AWidth, ALength, AReload) {
    layer.open({
        type: 2,
        title: ACaption,
        shadeClose: false,
        shade: 0.7,//透明度
        area: [AWidth, ALength],
        maxmin: true,
        fixed: false, //不固定
        moveOut: true,
        content: AUrl,
        end: function () {
            //默认参数为0，不刷心
            AReload = AReload || 0;
            if (AReload == 1) {
                window.location.reload();
            }
        }
    });
}

//弹出一个HTML窗口
function ShowLayerHTMLWindowMore(ACaption, AUrl, AWidth, ALength, AReload) {
    layer.open({
        type: 2,
        title: ACaption,
        shadeClose: false,
        shade: 0,//透明度
        tipsMore: true,
        area: [AWidth, ALength],
        maxmin: true,
        fixed: false, //不固定
        moveOut: true,
        content: AUrl,
        end: function () {
            //默认参数为0，不刷心
            AReload = AReload || 0;
            if (AReload == 1) {
                window.location.reload();
            }
        }
    });
}

//根据回车数，来修改textarea编辑框的高度，使之自适应
function getCount(AID) {
    var count = document.all(AID).innerHTML.split('\n').length - 1;//获取回车的个数
    document.getElementById(AID).rows = count + 1;
}

//改变ID的颜色
function SetColor($AID, $AColor) {
    var d = document.getElementById($AID);
    d.style.color = $AColor;
}

//置顶
function SetTop($AID, $AURL) {
    $j($AID).click(function () {
        $j.ajax({
            type: 'GET',
            url: $AURL,
            cache: false, //或者设置true
            success: function (data) {
                if (data == 'ON') {
                    SetColor($AID.substring(1), "#FF0000");
                } else {
                    SetColor($AID.substring(1), "#000000");
                }
            }
        });
    });
}

//表单校验_是否为空
function checkemptybyid($AIINPUTID, $AOUTPUTID, $AMSG) {
    var str = document.getElementById($AIINPUTID).value;
    if (str == "") {
        document.getElementById($AOUTPUTID).innerHTML = $AMSG;
        return 1;
    } else {
        return 0
    }
}

//在Jquery里格式化Date日期时间数据
function DateFormat(time) {
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    //return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    return year + "-" + month + "-" + date;
}

//设置日期控件的时间
function SetCalendarDate(AID, ATEXT) {
    if ((ATEXT === '') || (ATEXT.trim().length === 0)) {
        document.getElementById(AID).value = DateFormat(new Date().getTime());
    } else {
        document.getElementById(AID).value = ATEXT;
    }
}